// 通过DOM事件发送消息给content-script
(function() {

    var h = document.documentElement.clientHeight || document.body.clientHeight;
    var e2=document.getElementById("show_time");
    e2.style.marginTop=h/10-10+"px";

    var el=document.getElementById("wrapper")
	var hel=document.getElementById("hello")
	changeBgc(el)
	setHello(hel)

    setInterval(() => {
        showtime();
    }, 1000);

    init();

    $("#tea_notebooks").blur(function () {
        setCookie("note",$("#tea_notebooks").val())
    })

    document.onkeydown=function(){
        if (event.keyCode == 13&&$("#input_search").is(":focus")){
            window.open("https://www.baidu.com/s?wd="+$("#input_search").val());
        }
    }




})();

function init() {
    $("#tea_notebooks").html(getCookie("note"));
    $.get("http://www.box3.cn/developtoolbox/online.php",function (data) {
        var obj = JSON.parse(data);
        $("#myip").html("您的IP："+obj.ip);
        $("#jinju").html(obj.msg);
    })
}


var e3=document.getElementById("time_span");
function showtime() {
    var date=new Date();
    var nian = date.getFullYear();
    var month = date.getMonth();
    var nowdate = date.getDate();
    var xingqi = date.getDay();

    var hour = date.getHours();
    var minute = date.getMinutes();
    var getSeconds = date.getSeconds();
    e3.innerHTML  = nian+"年 "+ (parseInt(month)+1)+"月 "+nowdate+"日<hr>" +
        hour+"时 "+minute+"分 "+getSeconds+"秒 <hr> 星期"+xingqi;
}

$("#search_baidu").click(function () {
    window.open("https://www.baidu.com/s?wd="+$("#input_search").val());
})

$("#search_wrb").click(function () {
    window.open("https://cn.bing.com/search?q="+$("#input_search").val());
})

function changeBgc(el){
	// console.log("bgDrawing")
	var num=parseInt(Math.random()*23);
	num?num:22;
	var path='img/backgrounds/'+num+'.jpg'
	el.style.backgroundImage="url("+path+")";
}

function setHello(el){
	var date=new Date();
	var hour=date.getHours();
	var text="";
	if(hour<=11){
		text="Hello, Good Morning!"
	}else if(hour<=14){
		text="Hello, Good Noon!"
	}else if(hour<=18){
		text="Hello, Good Afternoon!"
	}else{
		text="Hello, Good Evening!"
	}
	el.innerHTML=text;
}

//存cookie
function setCookie(name,value)
{
    var Days = 5;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000*365);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//取cookie
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return "记事本\n";
}